package cn.guetzjb.sys.controller;

import cn.guet.log.anotation.Log;
import cn.guet.log.enums.BusinessType;
import cn.guetzjb.core.entity.R;
import cn.guetzjb.security.anotation.RequiresPermissions;
import cn.guetzjb.security.anotation.RequiresRoles;
import cn.guetzjb.security.utils.SecurityUtils;
import cn.guetzjb.sys.domain.SysDept;
import cn.guetzjb.sys.domain.SysUser;
import cn.guetzjb.sys.service.SysDeptService;
import cn.guetzjb.sys.service.SysUserService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Tag(name = "部门管理", description = "部门管理")
@RequiredArgsConstructor
public class DeptController {

    private final SysDeptService deptService;
    private final SysUserService userService;

    @GetMapping("/all/plain")
    @RequiresPermissions(value = {"dept:query"})
    @Operation(summary = "部门列表(平铺)")
    public R all() {
        return R.ok(deptService.list());
    }

    @GetMapping("/all/tree")
    @RequiresPermissions(value = {"dept:query"})
    @Operation(summary = "部门列表(树形)")
    public R allTree() {
        return R.ok(deptService.getTreeDeptList());
    }

    @PostMapping("/add")
    @RequiresPermissions(value = "dept:add")
    @Operation(summary = "新增部门")
    @Log(title = "新增部门", businessType = BusinessType.INSERT)
    public R add(@RequestBody SysDept sysDept) {
        if (sysDept.getParentId() == null) {
            sysDept.setParentId(0L);
        }
        sysDept.setCreateBy(SecurityUtils.getUsername());
        sysDept.setCreateTime(new Date());
        boolean b = deptService.saveOrUpdate(sysDept);
        if (b) {
            return R.ok("新增成功");
        }
        return R.failed();
    }

    @PostMapping("/delete/{ids}")
    @RequiresPermissions(value = "dept:delete")
    @Operation(summary = "删除部门")
    @Log(title = "删除部门", businessType = BusinessType.DELETE)
    public R delete(@PathVariable List<Integer> ids) {
        boolean b = deptService.removeBatchByIds(ids);
        if (b) {
            return R.ok("删除成功");
        }
        return R.failed();
    }

    @PostMapping("/update")
    @RequiresPermissions(value = "dept:update")
    @Operation(summary = "修改信息")
    @Log(title = "修改部门信息", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysDept sysDept) {
        boolean b = deptService.updateById(sysDept);
        if (b) {
            return R.ok("修改成功");
        }
        return R.failed();
    }

    @GetMapping("/user/dept")
    @RequiresPermissions(value = "dept:query")
    @Operation(summary = "部门用户列表")
    public R getDeptUserAll(@RequestParam(required = false, defaultValue = "1") Integer current,
                            @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "-1") Long deptId) {
        return R.ok(deptService.getUserListByDeptId(new Page<>(current, pageSize), deptId));
    }
}
